package com.jeecg.p3.baseApi.util;

import cn.hutool.core.codec.Base64Decoder;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author zhuzhe
 * @date 2018/10/17 17:21
 * @email zhe.zhu1@outlook.com
 */
public class Base64Utils {

    public static MultipartFile base64ToMultipart(String base64) {
        try {
            String[] baseStr = base64.split(",");

            Base64Decoder decoder = new Base64Decoder();
            byte[] b = new byte[0];
           b= Base64Decoder.decode(baseStr[1]);
            for(int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new Base64DecodeMultipartFile(b, baseStr[0]);
        } catch (Exception e) {
            return null;
        }
    }
}
